/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Clientes.java
 *
 * Created on 07-jun-2012, 22:15:58
 */
package megamar;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.Normalizer.Form;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Lizondo1
 */
public class Clientes extends javax.swing.JDialog {
    public String idzona;
    private boolean altacliente;    //si altacliente = true -> se puede dar de alta al cliente
    /** Creates new form Clientes */
    public Clientes(String id, java.awt.Frame parent, boolean modal) {
        super(parent,modal);
        initComponents();
        this.setSize(new Dimension(420, 650)); 
        jpanelmodificarcliente.setVisible(false);
        this.setLocationRelativeTo(null);
        idzona = id;
        completaridcliente();
        completarcomborubro();
        txtidcliente.setVisible(false);
        jLabel1.setVisible(false);
        txtdni.requestFocus();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jpanelnuevocliente = new javax.swing.JPanel();
        jbguardar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        txtidcliente = new javax.swing.JTextField();
        txtdni = new javax.swing.JTextField();
        txtnombre = new javax.swing.JTextField();
        txtapellido = new javax.swing.JTextField();
        txtdomicilioparticular = new javax.swing.JTextField();
        txtbarrioparticular = new javax.swing.JTextField();
        txtdomiciliocomercial = new javax.swing.JTextField();
        txtbarriocomercial = new javax.swing.JTextField();
        txttelefono = new javax.swing.JTextField();
        comborubro = new javax.swing.JComboBox();
        jpanelmodificarcliente = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        jPanel10 = new javax.swing.JPanel();
        txtidcliente1 = new javax.swing.JTextField();
        txtdni1 = new javax.swing.JTextField();
        txtnombre1 = new javax.swing.JTextField();
        txtapellido1 = new javax.swing.JTextField();
        txtdomicilioparticular1 = new javax.swing.JTextField();
        txtbarrioparticular1 = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jLabel28 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();
        jLabel30 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        txtdomiciliocomercial1 = new javax.swing.JTextField();
        txtbarriocomercial1 = new javax.swing.JTextField();
        txttelefono1 = new javax.swing.JTextField();
        comboperno = new javax.swing.JComboBox();
        comborubro1 = new javax.swing.JComboBox();
        txtnumcreditos = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel15 = new javax.swing.JLabel();
        txtcodigocliente = new javax.swing.JTextField();
        jbbuscar = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jbeliminarcliente = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jbguardarcambios = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gestion de Clientes");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/cliente_nuevo_48.png"))); // NOI18N
        jButton1.setText("Nuevo Cliente");
        jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setIconTextGap(1);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/cliente_editar_48.png"))); // NOI18N
        jButton4.setText("Modificar Cliente");
        jButton4.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setIconTextGap(1);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton4)
                .addContainerGap(482, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpanelnuevocliente.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jpanelnuevocliente.setName("jpanelnuevocliente"); // NOI18N

        jbguardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/save_32.png"))); // NOI18N
        jbguardar.setText("Guardar");
        jbguardar.setPreferredSize(new java.awt.Dimension(125, 41));
        jbguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbguardarActionPerformed(evt);
            }
        });
        jbguardar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jbguardarKeyTyped(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/Cancel.png"))); // NOI18N
        jButton3.setText("Cancelar");
        jButton3.setPreferredSize(new java.awt.Dimension(125, 41));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos del Cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));

        jPanel6.setLayout(new java.awt.GridLayout(10, 0, 5, 5));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Código");
        jPanel6.add(jLabel1);

        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel16.setText("DNI *");
        jPanel6.add(jLabel16);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Nombre *");
        jPanel6.add(jLabel2);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Apellido *");
        jPanel6.add(jLabel3);

        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel17.setText("Domicilio Particular");
        jPanel6.add(jLabel17);

        jLabel20.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel20.setText("Barrio Particular");
        jPanel6.add(jLabel20);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Domicilio Comercial");
        jPanel6.add(jLabel6);

        jLabel21.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel21.setText("Barrio Comercial");
        jPanel6.add(jLabel21);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Teléfono");
        jPanel6.add(jLabel5);

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Rubro *");
        jPanel6.add(jLabel7);

        jPanel5.setLayout(new java.awt.GridLayout(10, 0, 5, 5));

        txtidcliente.setEditable(false);
        jPanel5.add(txtidcliente);

        txtdni.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtdniKeyTyped(evt);
            }
        });
        jPanel5.add(txtdni);

        txtnombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtnombreKeyTyped(evt);
            }
        });
        jPanel5.add(txtnombre);

        txtapellido.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtapellidoKeyTyped(evt);
            }
        });
        jPanel5.add(txtapellido);

        txtdomicilioparticular.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtdomicilioparticularKeyTyped(evt);
            }
        });
        jPanel5.add(txtdomicilioparticular);

        txtbarrioparticular.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtbarrioparticularKeyTyped(evt);
            }
        });
        jPanel5.add(txtbarrioparticular);

        txtdomiciliocomercial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtdomiciliocomercialKeyTyped(evt);
            }
        });
        jPanel5.add(txtdomiciliocomercial);

        txtbarriocomercial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtbarriocomercialKeyTyped(evt);
            }
        });
        jPanel5.add(txtbarriocomercial);

        txttelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txttelefonoKeyTyped(evt);
            }
        });
        jPanel5.add(txttelefono);

        comborubro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                comborubroKeyTyped(evt);
            }
        });
        jPanel5.add(comborubro);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jpanelnuevoclienteLayout = new javax.swing.GroupLayout(jpanelnuevocliente);
        jpanelnuevocliente.setLayout(jpanelnuevoclienteLayout);
        jpanelnuevoclienteLayout.setHorizontalGroup(
            jpanelnuevoclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanelnuevoclienteLayout.createSequentialGroup()
                .addGroup(jpanelnuevoclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpanelnuevoclienteLayout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbguardar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpanelnuevoclienteLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpanelnuevoclienteLayout.setVerticalGroup(
            jpanelnuevoclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanelnuevoclienteLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanelnuevoclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbguardar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpanelmodificarcliente.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jpanelmodificarcliente.setName("jpanelnuevocliente"); // NOI18N

        jLabel8.setText("Busque el cliente por codigo y luego modifique los datos que desee.");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Cliente"));

        jPanel9.setLayout(new java.awt.GridLayout(6, 0, 5, 5));

        jLabel22.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel22.setText("Código");
        jPanel9.add(jLabel22);

        jLabel23.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel23.setText("DNI *");
        jPanel9.add(jLabel23);

        jLabel24.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel24.setText("Nombre *");
        jPanel9.add(jLabel24);

        jLabel25.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel25.setText("Apellido *");
        jPanel9.add(jLabel25);

        jLabel26.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel26.setText("Domicilio Particular");
        jPanel9.add(jLabel26);

        jLabel27.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel27.setText("Barrio Particular");
        jPanel9.add(jLabel27);

        jPanel10.setLayout(new java.awt.GridLayout(6, 0, 5, 5));

        txtidcliente1.setEditable(false);
        txtidcliente1.setFocusable(false);
        jPanel10.add(txtidcliente1);

        txtdni1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtdni1KeyTyped(evt);
            }
        });
        jPanel10.add(txtdni1);

        txtnombre1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtnombre1KeyTyped(evt);
            }
        });
        jPanel10.add(txtnombre1);

        txtapellido1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtapellido1KeyTyped(evt);
            }
        });
        jPanel10.add(txtapellido1);

        txtdomicilioparticular1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtdomicilioparticular1KeyTyped(evt);
            }
        });
        jPanel10.add(txtdomicilioparticular1);

        txtbarrioparticular1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtbarrioparticular1KeyTyped(evt);
            }
        });
        jPanel10.add(txtbarrioparticular1);

        jPanel7.setLayout(new java.awt.GridLayout(6, 0, 5, 5));

        jLabel28.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel28.setText("Domicilio Comercial");
        jPanel7.add(jLabel28);

        jLabel29.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel29.setText("Barrio Comercial");
        jPanel7.add(jLabel29);

        jLabel30.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel30.setText("Teléfono");
        jPanel7.add(jLabel30);

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Perno");
        jPanel7.add(jLabel9);

        jLabel31.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel31.setText("Rubro *");
        jPanel7.add(jLabel31);

        jLabel4.setText("Número de Creditos");
        jPanel7.add(jLabel4);

        jPanel8.setLayout(new java.awt.GridLayout(6, 0, 5, 5));

        txtdomiciliocomercial1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtdomiciliocomercial1KeyTyped(evt);
            }
        });
        jPanel8.add(txtdomiciliocomercial1);

        txtbarriocomercial1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtbarriocomercial1KeyTyped(evt);
            }
        });
        jPanel8.add(txtbarriocomercial1);

        txttelefono1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txttelefono1KeyTyped(evt);
            }
        });
        jPanel8.add(txttelefono1);

        comboperno.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SI", "NO" }));
        comboperno.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                combopernoKeyTyped(evt);
            }
        });
        jPanel8.add(comboperno);

        comborubro1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                comborubro1KeyTyped(evt);
            }
        });
        jPanel8.add(comborubro1);

        txtnumcreditos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtnumcreditosKeyTyped(evt);
            }
        });
        jPanel8.add(txtnumcreditos);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(38, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel9, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Buscar Cliente"));

        jLabel15.setText("Codigo");

        txtcodigocliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtcodigoclienteKeyPressed(evt);
            }
        });

        jbbuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/Sherlocks_Tool.png"))); // NOI18N
        jbbuscar.setText("Buscar");
        jbbuscar.setPreferredSize(new java.awt.Dimension(111, 41));
        jbbuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbbuscarActionPerformed(evt);
            }
        });

        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/clientes_32.png"))); // NOI18N
        jButton8.setText("Ver Listado de Clientes");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel15)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtcodigocliente, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbbuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton8)
                .addContainerGap(244, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(jbbuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtcodigocliente)
                    .addComponent(jButton8))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jbeliminarcliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/Delete.png"))); // NOI18N
        jbeliminarcliente.setText("Eliminar Cliente");
        jbeliminarcliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbeliminarclienteActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/acerca.png"))); // NOI18N
        jButton2.setText("Cambiar Zona");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jbguardarcambios.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/save_32.png"))); // NOI18N
        jbguardarcambios.setText("Guardar Cambios");
        jbguardarcambios.setPreferredSize(new java.awt.Dimension(180, 41));
        jbguardarcambios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbguardarcambiosActionPerformed(evt);
            }
        });
        jbguardarcambios.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jbguardarcambiosKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jpanelmodificarclienteLayout = new javax.swing.GroupLayout(jpanelmodificarcliente);
        jpanelmodificarcliente.setLayout(jpanelmodificarclienteLayout);
        jpanelmodificarclienteLayout.setHorizontalGroup(
            jpanelmodificarclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanelmodificarclienteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanelmodificarclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(jpanelmodificarclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jpanelmodificarclienteLayout.createSequentialGroup()
                            .addComponent(jButton2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbeliminarcliente)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbguardarcambios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jpanelmodificarclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jpanelmodificarclienteLayout.setVerticalGroup(
            jpanelmodificarclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanelmodificarclienteLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanelmodificarclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbguardarcambios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbeliminarcliente)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jpanelnuevocliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jpanelmodificarcliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpanelnuevocliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpanelmodificarcliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(45, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbguardarActionPerformed
    String nombre = txtnombre.getText();
    String apellido = txtapellido.getText();
    String domicilio_particular = txtdomicilioparticular.getText();
    String barrio_particular = txtbarrioparticular.getText();
    String domicilio_comercial = txtdomiciliocomercial.getText();
    String barrio_comercial = txtbarriocomercial.getText();
    String telefono = txttelefono.getText();
    int num_creditos = 0;   //Cuando se da de alta un cliente se guarda como /0
    String perno = "NO";    //Perno = 0
    String estado = "NUEVO";
    int rubro = comborubro.getSelectedIndex();
    //String idz = idzona;
    
    if(txtdni.getText().equals("")){
        JOptionPane.showMessageDialog(null,"Ingrese el DNI del Cliente.","Error!",JOptionPane.ERROR_MESSAGE);
        txtdni.requestFocus();
        return;
    }
    if(existedni(txtdni.getText())){
        txtdni.requestFocus();
        return;
    }
    if(nombre.equals("")){
        JOptionPane.showMessageDialog(null,"Ingrese el Nombre del Cliente.","Error!",JOptionPane.ERROR_MESSAGE);
        txtnombre.requestFocus();
        return;
    }
    if(apellido.equals("")){
        JOptionPane.showMessageDialog(null,"Ingrese el Apellido del Cliente.","Error!",JOptionPane.ERROR_MESSAGE);
        txtapellido.requestFocus();
        return;
    }
    if(rubro == 0){
        JOptionPane.showMessageDialog(null,"Seleccione un Rubro de la lista.","Error!",JOptionPane.ERROR_MESSAGE);
        comborubro.requestFocus();
        return;
    }
    if (altacliente == false) {
        JOptionPane.showMessageDialog(null, "Error! No se pude dar de alta al cliente porque ya se encuentra registrado en la Base de Datos.", "Error!", JOptionPane.ERROR_MESSAGE);
        return;
    }
    
    //int idcliente = Integer.parseInt(txtidcliente.getText());
    int dni = Integer.parseInt(txtdni.getText());
    
    String consulta = "insert into cliente(dni, nombre, apellido, domicilio_particular, barrio_particular, domicilio_comercial, barrio_comercial, telefono, num_creditos, perno, estado, idzona, idrubro) "
            + "values('"+dni+"', '"+nombre+"', '"+apellido+"', '"+domicilio_particular+"', '"+barrio_particular+"', '"+domicilio_comercial+"', '"+barrio_comercial+"', "
            + " '"+telefono+"', '"+num_creditos+"', '"+perno+"', '"+estado+"', '"+idzona+"', '"+rubro+"' )";
    try{
          Conectar();
            int done= stmt.executeUpdate(consulta);
            //JOptionPane.showMessageDialog(null,"alta","Aviso",JOptionPane.INFORMATION_MESSAGE);
            JOptionPane.showMessageDialog(null,"Se guardaron los datos del nuevo cliente","Aviso", JOptionPane.INFORMATION_MESSAGE);
            db.close(stmt);
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"Error! No se pudo cargar los datos del cliente",JOptionPane.ERROR_MESSAGE);
        }
      db.destroy();
      this.dispose();
}//GEN-LAST:event_jbguardarActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    this.dispose();
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    this.setSize(new Dimension(420, 650));
    this.setLocationRelativeTo(null);
    jpanelnuevocliente.setVisible(true);
    jpanelmodificarcliente.setVisible(false);
    txtdni.requestFocus();
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    this.setSize(new Dimension(800, 570));
    this.setLocationRelativeTo(null);
    jpanelnuevocliente.setVisible(false);
    jpanelmodificarcliente.setVisible(true);
    txtcodigocliente.requestFocus();
}//GEN-LAST:event_jButton4ActionPerformed

private void jbguardarcambiosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbguardarcambiosActionPerformed
    if(txtdni1.getText().equals("")){
        JOptionPane.showMessageDialog(null,"Ingrese el DNI del Cliente.","Error!",JOptionPane.ERROR_MESSAGE);
        txtdni1.requestFocus();
        return;
    }
    
    int idcliente = Integer.parseInt(txtidcliente1.getText());
    int dni = Integer.parseInt(txtdni1.getText());
    String nombre = txtnombre1.getText();
    String apellido = txtapellido1.getText();
    String domicilio_particular = txtdomicilioparticular1.getText();
    String barrio_particular = txtbarrioparticular1.getText();
    String domicilio_comercial = txtdomiciliocomercial1.getText();
    String barrio_comercial = txtbarriocomercial1.getText();
    String telefono = txttelefono1.getText();
    String num_creditos = txtnumcreditos.getText();
    

    String perno = comboperno.getSelectedItem().toString();
    int rubro = comborubro1.getSelectedIndex();
    
    if(nombre.equals("")){
        JOptionPane.showMessageDialog(null,"Ingrese el Nombre del Cliente.","Error!",JOptionPane.ERROR_MESSAGE);
        txtnombre1.requestFocus();
        return;
    }
    if(apellido.equals("")){
        JOptionPane.showMessageDialog(null,"Ingrese el Apellido del Cliente.","Error!",JOptionPane.ERROR_MESSAGE);
        txtapellido1.requestFocus();
        return;
    }
    if(rubro == 0){
        JOptionPane.showMessageDialog(null,"Seleccione un Rubro de la lista.","Error!",JOptionPane.ERROR_MESSAGE);
        comborubro1.requestFocus();
        return;
    }

    String consulta = "update cliente "
            + "set dni='" + dni + "', nombre='" + nombre + "', apellido='" + apellido + "', domicilio_particular='" + domicilio_particular + "', barrio_particular='" + barrio_particular + "', domicilio_comercial='" + domicilio_comercial + "', barrio_comercial='" + barrio_comercial + "', telefono='" + telefono + "', perno='" + perno + "', idrubro='" + rubro + "', num_creditos='" + num_creditos + "' "
            + "where idcliente='" + idcliente + "'";

    try {
        Conectar();
        int done = stmt.executeUpdate(consulta);
        //JOptionPane.showMessageDialog(null,"alta","Aviso",JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(null, "Se actualizaron los datos del cliente.", "Aviso", JOptionPane.INFORMATION_MESSAGE);
        db.close(stmt);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "Error! No se pudo actualizar los datos del cliente.", JOptionPane.ERROR_MESSAGE);
    }
    db.destroy();
    this.dispose();
}//GEN-LAST:event_jbguardarcambiosActionPerformed

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
    ListadoClientes L = new ListadoClientes(idzona,this,true);
    L.setVisible(true);
    if (L.getidcliente().equals("")) {
        //return;
    } else {
        txtcodigocliente.setText(L.getidcliente());
        cargardatos();
    }
}//GEN-LAST:event_jButton8ActionPerformed

    private void jbbuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbbuscarActionPerformed
        cargardatos();
    }//GEN-LAST:event_jbbuscarActionPerformed

private void txtdniKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdniKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 8) {
        return;
    }
    if (k == 10) {
        txtdni.transferFocus();         //transfiere el foco si presionas enter
    } else if (!Character.isDigit(k) || txtdni.getText().length() > 7) {
        evt.setKeyChar((char) KeyEvent.VK_CLEAR);
    }
}//GEN-LAST:event_txtdniKeyTyped

private void txtnombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnombreKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtnombre.transferFocus();
    }
}//GEN-LAST:event_txtnombreKeyTyped

private void txtapellidoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtapellidoKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtapellido.transferFocus();
    }
}//GEN-LAST:event_txtapellidoKeyTyped

private void txtdomicilioparticularKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdomicilioparticularKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtdomicilioparticular.transferFocus();
    }
}//GEN-LAST:event_txtdomicilioparticularKeyTyped

private void txtbarrioparticularKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtbarrioparticularKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtbarrioparticular.transferFocus();
    }
}//GEN-LAST:event_txtbarrioparticularKeyTyped

private void txtdomiciliocomercialKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdomiciliocomercialKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtdomiciliocomercial.transferFocus();
    }
}//GEN-LAST:event_txtdomiciliocomercialKeyTyped

private void txtbarriocomercialKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtbarriocomercialKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtbarriocomercial.transferFocus();
    }
}//GEN-LAST:event_txtbarriocomercialKeyTyped

private void comborubroKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_comborubroKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        jbguardar.requestFocus();
    }
}//GEN-LAST:event_comborubroKeyTyped

private void txtdni1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdni1KeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 8) {
        return;
    }
    if (k == 10) {
        if(!existedni(txtdni.getText())){
            txtdni1.transferFocus();         //transfiere el foco si presionas enter
        }
    } else if (!Character.isDigit(k) || txtdni1.getText().length() > 7) {
        evt.setKeyChar((char) KeyEvent.VK_CLEAR);
    }
}//GEN-LAST:event_txtdni1KeyTyped

private void txtnombre1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnombre1KeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtnombre1.transferFocus();
    }
}//GEN-LAST:event_txtnombre1KeyTyped

private void txtapellido1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtapellido1KeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtapellido1.transferFocus();
    }
}//GEN-LAST:event_txtapellido1KeyTyped

private void txtdomicilioparticular1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdomicilioparticular1KeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtdomicilioparticular1.transferFocus();
    }
}//GEN-LAST:event_txtdomicilioparticular1KeyTyped

private void txtbarrioparticular1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtbarrioparticular1KeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtbarrioparticular1.transferFocus();
    }
}//GEN-LAST:event_txtbarrioparticular1KeyTyped

private void txtdomiciliocomercial1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdomiciliocomercial1KeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtdomiciliocomercial1.transferFocus();
    }
}//GEN-LAST:event_txtdomiciliocomercial1KeyTyped

private void txtbarriocomercial1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtbarriocomercial1KeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtbarriocomercial1.transferFocus();
    }
}//GEN-LAST:event_txtbarriocomercial1KeyTyped

private void combopernoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_combopernoKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        comboperno.transferFocus();
    }
}//GEN-LAST:event_combopernoKeyTyped

private void comborubro1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_comborubro1KeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        txtnumcreditos.requestFocus();
    }
}//GEN-LAST:event_comborubro1KeyTyped

private void jbguardarcambiosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbguardarcambiosKeyTyped
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        jbguardarcambios.doClick();
    }
}//GEN-LAST:event_jbguardarcambiosKeyTyped

private void txtcodigoclienteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtcodigoclienteKeyPressed
    int k = (int) evt.getKeyChar();
    if (k == 10) {
        //transfiere el foco si presionas enter
        jbbuscar.doClick();
        txtdni1.requestFocus();
    }
}//GEN-LAST:event_txtcodigoclienteKeyPressed

private void jbeliminarclienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbeliminarclienteActionPerformed
    String consulta = "Select * from credito where idcliente = '" + txtidcliente1.getText() + "' and saldo > 0";
    try {
        Conectar();
        ResultSet rs = stmt.executeQuery(consulta);
        rs.last();
        if (rs.getRow() == 0) {
            int res = JOptionPane.showConfirmDialog(null, "Realmente desea eliminar el Cliente seleccionado?");
            if (res == JOptionPane.OK_OPTION) {
                consulta = "Delete from cliente where idcliente='" + txtidcliente1.getText() + "' ";
                int done = stmt.executeUpdate(consulta);
                consulta = "Delete from credito where idcliente='" + txtidcliente1.getText() + "' ";
                done = stmt.executeUpdate(consulta);
                consulta = "Delete from pago where idcliente='" + txtidcliente1.getText() + "' ";
                done = stmt.executeUpdate(consulta);
                JOptionPane.showMessageDialog(null, "Se eliminaron todos los datos del Cliente seleccionado.","Baja de Cliente", JOptionPane.INFORMATION_MESSAGE);
            }
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "No se puede eliminar al Cliente seleccionado porque registra creditos impagos.\nVerifique el historial de los Creditos del Cliente.","Error", JOptionPane.ERROR_MESSAGE);
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Error al verificar los Creditos del Cliente", "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jbeliminarclienteActionPerformed

    private void jbguardarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbguardarKeyTyped
        int k = (int) evt.getKeyChar();
        if (k == 10) {
            //transfiere el foco si presionas enter
            jbguardar.doClick();
        }
    }//GEN-LAST:event_jbguardarKeyTyped

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ClienteCambiarZona C = new ClienteCambiarZona(this, true, txtidcliente1.getText());
        C.setVisible(true);
        if (C.getcambiozona() == true) {
            this.dispose();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void txttelefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txttelefonoKeyTyped
        int k = (int) evt.getKeyChar();
        if (k == 10) {
            //transfiere el foco si presionas enter
            txttelefono.transferFocus();
        }

    }//GEN-LAST:event_txttelefonoKeyTyped

    private void txttelefono1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txttelefono1KeyTyped
        int k = (int) evt.getKeyChar();
        if (k == 10) {
            //transfiere el foco si presionas enter
            txttelefono1.transferFocus();
        }

    }//GEN-LAST:event_txttelefono1KeyTyped

    private void txtnumcreditosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnumcreditosKeyTyped
        int k = (int) evt.getKeyChar();
        if (k == 10) {
            //transfiere el foco si presionas enter
            jbguardarcambios.requestFocus();
        }
    }//GEN-LAST:event_txtnumcreditosKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        /*java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Clientes().setVisible(true);
            }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboperno;
    private javax.swing.JComboBox comborubro;
    private javax.swing.JComboBox comborubro1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JButton jbbuscar;
    private javax.swing.JButton jbeliminarcliente;
    private javax.swing.JButton jbguardar;
    private javax.swing.JButton jbguardarcambios;
    private javax.swing.JPanel jpanelmodificarcliente;
    private javax.swing.JPanel jpanelnuevocliente;
    private javax.swing.JTextField txtapellido;
    private javax.swing.JTextField txtapellido1;
    private javax.swing.JTextField txtbarriocomercial;
    private javax.swing.JTextField txtbarriocomercial1;
    private javax.swing.JTextField txtbarrioparticular;
    private javax.swing.JTextField txtbarrioparticular1;
    private javax.swing.JTextField txtcodigocliente;
    private javax.swing.JTextField txtdni;
    private javax.swing.JTextField txtdni1;
    private javax.swing.JTextField txtdomiciliocomercial;
    private javax.swing.JTextField txtdomiciliocomercial1;
    private javax.swing.JTextField txtdomicilioparticular;
    private javax.swing.JTextField txtdomicilioparticular1;
    private javax.swing.JTextField txtidcliente;
    private javax.swing.JTextField txtidcliente1;
    private javax.swing.JTextField txtnombre;
    private javax.swing.JTextField txtnombre1;
    private javax.swing.JTextField txtnumcreditos;
    private javax.swing.JTextField txttelefono;
    private javax.swing.JTextField txttelefono1;
    // End of variables declaration//GEN-END:variables
    private conexion db;
    private Statement stmt;
    
    public void Conectar() {
        try {
            db = new conexion();      //instancia de la clase conexion.java
            db.init();
            Connection conn = db.getMyConnection();
            stmt = conn.createStatement();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Problemas al concetarse a la Base de Datos", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void completaridcliente() {
        String id;
        String consulta = "Select * from cliente ";
        try {
            Conectar();
            ResultSet rs = stmt.executeQuery(consulta);
            if(rs.last()){
                id = Integer.toString(rs.getInt(1)+1);
            }else{
                id = "1";       //El primer registro de la tabla
            }
            txtidcliente.setText(id);
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error al cargar el codigo del cliente.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void completarcomborubro() {
            String consulta = "Select descripcion from rubro ";
        try {
            Conectar();
            ResultSet rs = stmt.executeQuery(consulta);
            rs.beforeFirst();
            comborubro.addItem("-");
            comborubro1.addItem("-");
            while (rs.next()) {
                comborubro.addItem(rs.getString(1));
                comborubro1.addItem(rs.getString(1));
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error al cargar los RUBROS.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void cargardatos() {
        String consulta = "Select * from cliente where idcliente = '" + txtcodigocliente.getText() + "' and idzona = '"+idzona+"'";
        try {
            Conectar();
            ResultSet rs = stmt.executeQuery(consulta);
            rs.last();
            //System.out.println(rs.getRow());
            if (rs.getRow() == 0) {
                JOptionPane.showMessageDialog(null, "No existe ningun cliente con ese codigo. Por favor verifique el numero ingresado. ", "Error", JOptionPane.ERROR_MESSAGE);
                limpiarcampos();
            } else {
                rs.beforeFirst();
                while (rs.next()) {
                    txtidcliente1.setText(rs.getString("idcliente"));
                    txtdni1.setText(rs.getString("dni"));
                    txtnombre1.setText(rs.getString("nombre"));
                    txtapellido1.setText(rs.getString("apellido"));
                    txtdomicilioparticular1.setText(rs.getString("domicilio_particular"));
                    txtbarrioparticular1.setText(rs.getString("barrio_particular"));
                    txtdomiciliocomercial1.setText(rs.getString("domicilio_comercial"));
                    txtbarriocomercial1.setText(rs.getString("barrio_comercial"));
                    txttelefono1.setText(rs.getString("telefono"));
                    txtnumcreditos.setText(rs.getString("num_creditos"));
                    if(rs.getString("perno").equals("SI")){
                        comboperno.setSelectedIndex(0);
                    }else{
                        comboperno.setSelectedIndex(1);
                    }
                    comborubro1.setSelectedIndex(rs.getInt("idrubro"));
                }
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error al cargar los datos del Cliente", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void limpiarcampos() {
        txtidcliente1.setText("");
        txtdni1.setText("");
        txtnombre1.setText("");
        txtapellido1.setText("");
        txtdomicilioparticular1.setText("");
        txtbarrioparticular1.setText("");
        txtdomiciliocomercial1.setText("");
        txtbarriocomercial1.setText("");
        txttelefono1.setText("");
        comborubro1.setSelectedIndex(0);
        txtnumcreditos.setText("");
    }

    private boolean existedni(String dni) {
        String consulta = "Select CONCAT(c.apellido,' ',c.nombre) as nombre, z.descripcion from cliente c, zona z where dni = '" + dni + "' and c.idzona=z.idzona ";
        try {
            Conectar();
            ResultSet rs = stmt.executeQuery(consulta);
            rs.last();
            //System.out.println(rs.getRow());
            if (rs.getRow() == 0) {
                //JOptionPane.showMessageDialog(null, "No existe ningun cliente con ese dni.", "OK", JOptionPane.ERROR_MESSAGE);
                altacliente = true;
                return false;
            } else {
                JOptionPane.showMessageDialog(null, "Ya existe un cliente cargado con ese DNI. \nCliente: "+rs.getString("nombre") +"\nZona: "+rs.getString("descripcion"), "Error", JOptionPane.ERROR_MESSAGE);
                altacliente = false;
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error al cargar los datos del Cliente", "Error", JOptionPane.ERROR_MESSAGE);
        }
        return true;
    }
}